package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.dao.*;
import org.dairy.farms.model.*;
import org.dairy.farms.nandana.web.view.LivestockView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Divya
 * Date: Jul 4, 2011
 * Time: 5:07:40 PM
 */
@Service(value = "livestockManagementLogic")
public class LivestockManagementLogicImpl implements LivestockManagementLogic {
    private static final Logger logger = LoggerFactory.getLogger(LivestockManagementLogicImpl.class);
    @Autowired
    private LivestockDAO livestockDao;
    @Autowired
    private FarmDAO farmDAO;
    @Autowired
    private LivestockStatusTypeDAO livestockStatusTypeDao;
    @Autowired
    private LivestockNotesDAO livestockNotesDAO;

    @Override
    public List<Livestock> searchLivestockBy(String searchBy, String searchParam, int page, int start, int limit) {
        List<Livestock> returnList = null;
        Object value = searchBy.equals("id")?new Long(searchParam):searchParam;
        returnList = livestockDao.findByProperty(searchBy,value,page,start,limit);
        return returnList;
    }

    @Override
    public int searchLivestockBy(String searchBy, String searchParam) {
        List<Livestock> returnList = null;
        Object value = searchBy.equals("id")?new Long(searchParam):searchParam;
        returnList = livestockDao.findByProperty(searchBy,value);
        return returnList.size();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Livestock createLivestock(LivestockView livestockView) {
        Livestock livestock = new Livestock();
        populateMotherAndFather(livestockView, livestock);
        Farm farm = farmDAO.findById(livestockView.getFarmId());
        livestock.setFarm(farm);
        livestock.setDateOfBirthDate(livestockView.getDateOfBirth());
        livestock.setName(livestockView.getName());
        livestock.setGender(livestockView.getGender());
        livestock.setGenetics(livestockView.getGenetics());
        livestock.setGivenId(livestockView.getGivenId());
        livestock.setHeight(livestockView.getHeight());
        livestock.setListPrice(livestockView.getListPrice());
        LivestockStatusType livestockStatusType = livestockStatusTypeDao.findById(livestockView.getLivestockStatusTypeId());
        livestock.setLivestockStatusType(livestockStatusType);
        livestock.setPurchaseDate(livestockView.getPurchaseDate());
        livestock.setPurchasePrice(livestockView.getPurchasePrice());
        livestock.setSalePrice(livestockView.getSellingPrice());
        livestock.setWeight(livestockView.getWeight());
        livestockDao.create(livestock);
        if (livestockView.getNotes() != null && !livestockView.getNotes().isEmpty()) {
            addLivestockNotes(livestock.getId(), livestockView.getNotes());
        }
        return livestock;
    }

    private void populateMotherAndFather(LivestockView livestockView, Livestock livestock) {
        Livestock mother = livestockDao.findById(livestockView.getMotherId());
        Livestock father = livestockDao.findById(livestockView.getFatherId());
        livestock.setMother(mother);
        livestock.setFather(father);
    }

    @Override
    public Notes addLivestockNotes(Long livestockId, String notes) {
        LivestockNotes livestockNotes = new LivestockNotes();
        livestockNotes.setLivestock(livestockDao.findById(livestockId));
        livestockNotes.setNote(notes);
        return livestockNotesDAO.create(livestockNotes);
    }

    @Override
    public List<Notes> getLivestockNotes(Long livestockId) {
        return livestockNotesDAO.findByProperty("livestock.id",livestockId);
    }

    public List<Livestock> getLivestockDetails(Long[] livestockId) {
        return livestockDao.findByIds(livestockId);
    }

    public List<LivestockStatusType> getLivestockStatusTypes() {
        return livestockStatusTypeDao.loadAllObjects();
    }

}
